#pragma once

#include <array>
#include <string>

namespace solution_003
{

class Solution
{
public:
    int lengthOfLongestSubstring(std::string s)
    {
        if (s.empty())
            return 0;

        int max_len = 1;
        int len = 1;

        std::array<int, 128> table;
        table.fill(-1);
        table[s.front()] = 0;

        for (int i = 0, j = 1; j < (int)s.size(); ++j) {
            auto ch = s.at(j);
            if (table.at(ch) < i) {
                ++len;
            } else {
                max_len = std::max(max_len, len);
                i = table.at(ch) + 1;
                len = j - i + 1;
            }
            table.at(ch) = j;
        }
        return std::max(max_len, len);
    }
};

}  /* namespace solution_003 */

